package gui;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

import control.AddPackListener;
import control.CardImageListener;
import control.ExportPoolListener;
import control.GeneratePoolListener;

import sealedGenerator.KeyFileReader;
import sealedGenerator.MWSPatchReader;
import sealedGenerator.Set;
import sealedGenerator.SetFormatException;
import sealedGenerator.card.Card;

public class StartView extends JPanel {
	private ArrayList<JComboBox> packsComboBoxes;
	private List<String> listOfSets;
	private final JPanel packsPanel;
	private KeyFileReader keyFile;
	private final JPanel output;
	private final JPanel imagePanel;
	public StartView(){
		this.setLayout(new BorderLayout());
		packsPanel = new JPanel();
		packsPanel.setLayout(new VerticalLayout());
		output = new JPanel();
		output.setLayout(new VerticalLayout());
		imagePanel=new JPanel();
		this.add(packsPanel, BorderLayout.WEST);
		this.add(output, BorderLayout.CENTER);
		this.add(imagePanel, BorderLayout.EAST);
		JButton generatePool=new JButton("Generate Sealed Pool");
		generatePool.addActionListener(new GeneratePoolListener(this));
		packsPanel.add(generatePool);
		JButton addPack=new JButton("Add Pack");
		addPack.addActionListener(new AddPackListener(this));
		packsPanel.add(addPack);
		packsComboBoxes=new ArrayList<JComboBox>();


		try {
			keyFile = new KeyFileReader();
			listOfSets = keyFile.getNames();
			JComboBox sets=new JComboBox(listOfSets.toArray());
			packsComboBoxes.add(sets);
			packsPanel.add(sets);
		} catch (FileNotFoundException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
	}
	public void addPack(){
		JComboBox newPack=new JComboBox(listOfSets.toArray());
		packsComboBoxes.add(newPack);
		packsPanel.add(newPack);
		packsPanel.updateUI();
	}
	public void generatePool(){
		//0=name, 1=file, 2=code, 3=booster format
		List<Card> cards=new ArrayList<Card>();
		for(JComboBox box : packsComboBoxes){
			try {
				String[] setInfo=keyFile.getSetInfo((String)box.getSelectedItem());
				Set theSet=MWSPatchReader.read(
						setInfo[1], setInfo[0], setInfo[2], setInfo[3]);
				List<Card> pack=theSet.generatePack();
				cards.addAll(pack);
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (SetFormatException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		output.removeAll();
		Card.sort("CRM", cards);
		JList cardJList=new JList(cards.toArray());
		cardJList.addListSelectionListener(new CardImageListener(cardJList, imagePanel));
		JScrollPane scrollPane=new JScrollPane(cardJList);
		scrollPane.setPreferredSize(new Dimension(500, 500));
		output.add(scrollPane);
		JButton exportPool=new JButton("Export Sealed Pool");
		exportPool.addActionListener(new ExportPoolListener(cards));
		output.add(exportPool);
		output.updateUI();
	}
}
